import 'package:flutter/material.dart';
import 'package:flutter/services.dart';

import '../constant/colors.dart';
import 'magic_image.dart';

/// 通用型头部标题栏
///
/// [title] 标题。
/// 设置了 [titleWidget]，将会忽略 [title]。
/// [backgroundColor] 导航栏背景色，默认为 [MagicColors.greyBackground]。
/// [hidesBack] 是否隐藏返回按钮。
AppBar titleBar(
  BuildContext context, {
  String? title,
  Widget? titleWidget,
  Color backgroundColor = MagicColors.greyBackground,
  bool hidesBack = false,
  SystemUiOverlayStyle? systemOverlayStyle = SystemUiOverlayStyle.dark,
  bool useWhiteBack = false,
  Widget? customBackImg,
  VoidCallback? onBack,
  List<Widget>? rightWidgets,
}) {
  Widget? tmpTitle;
  if (titleWidget != null) {
    tmpTitle = titleWidget;
  } else if (title != null) {
    tmpTitle = Text(
      title,
      textAlign: TextAlign.center,
      overflow: TextOverflow.ellipsis,
      style: const TextStyle(
        color: MagicColors.topTitle,
        fontSize: 18,
        fontWeight: FontWeight.w500,
      ),
    );
  }

  final tmpActions = rightWidgets;
  if (tmpActions != null && tmpActions.isNotEmpty) {
    tmpActions.add(const SizedBox(width: 16));
  }

  return AppBar(
    elevation: 0,
    backgroundColor: backgroundColor,
    systemOverlayStyle: systemOverlayStyle,
    automaticallyImplyLeading: false,
    centerTitle: true,
    title: tmpTitle,
    leading: hidesBack
        ? null
        : GestureDetector(
            onTap: onBack ??
                () {
                  Navigator.of(context).pop();
                },
            child: customBackImg ?? (useWhiteBack ? MagicImage.whiteBack : MagicImage.back),
          ),
    actions: tmpActions,
  );
}
